package org.maxgroundstation.groundstation.view.map;

import java.awt.Color;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.maxgroundstation.groundstation.GroundStation;
import org.maxgroundstation.groundstation.GroundStationEvent;
import org.maxgroundstation.groundstation.GroundStationListener;
import org.maxgroundstation.groundstation.comm.service.Service;
import org.maxgroundstation.groundstation.comm.service.ServiceEvent;
import org.maxgroundstation.groundstation.comm.service.ServiceListener;
import org.maxgroundstation.groundstation.comm.service.gps.GpsService;

/**
 * A map overlay for displaying the rover's GPS path.
 * @author Scott Davis
 * 
 * Copyright (C) 2010  Scott Davis
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
public class RoverPathMapOverlay implements MapOverlay, GroundStationListener, 
        ServiceListener {

    // The overlay ID.
    public static final String OVERLAY_ID = "Rover's Path";
    
    // The minimum range (meters) to display a GPS trail segment. 
    private static final double TRAIL_SEGMENT_RANGE = 1D;
    
    // Data members
    private List<GpsLocation> gpsLocCache;
    
    /**
     * Constructor
     * @param gs the ground station.
     */
    public RoverPathMapOverlay(GroundStation gs) {
        
        // Initialize data members.
        gpsLocCache = new ArrayList<GpsLocation>();
        
        // Add as ground station listener.
        gs.addGroundStationListener(this);
    }
    
    @Override
    public void drawMapOverlay(String baseMapStyle, Graphics2D g2d, GpsLocation currentLoc, int width, 
            int height, float zoom) {
        
        int xCenter = width / 2;
        int yCenter = height / 2;
        
        // Draw GPS locations.
        g2d.setColor(Color.GREEN);
        if (currentLoc != null) {
            GpsLocation prevLoc = null;
            List<GpsLocation> tempGpsLocList = new ArrayList<GpsLocation>(gpsLocCache);
            Iterator<GpsLocation> i = tempGpsLocList.iterator();
            while (i.hasNext()) {
                GpsLocation loc = i.next();
                if (prevLoc != null) {
                    if (MapUtil.getDistance(prevLoc, loc) > TRAIL_SEGMENT_RANGE) {
                        int prevLocX = xCenter - (int) (MapUtil.getXDiff(prevLoc, currentLoc) / zoom);
                        int prevLocY = (int) (MapUtil.getYDiff(prevLoc, currentLoc) / zoom) + yCenter;
                        int locX = xCenter - (int) (MapUtil.getXDiff(loc, currentLoc) / zoom);
                        int locY = (int) (MapUtil.getYDiff(loc, currentLoc) / zoom) + yCenter;
                        g2d.drawLine(prevLocX, prevLocY, locX, locY);
                        prevLoc = loc;
                    }
                }
                else {
                    prevLoc = loc;
                }
            }
        }
    }

    @Override
    public void groundStationUpdate(GroundStationEvent event) {
        // If services initialized from rover, check for GPS service.
        GroundStation gs = (GroundStation) event.getSource();
        if (GroundStationEvent.SERVICES_INITIALIZED.equals(event.getType())) {
            if (gs.getServiceComm().hasService(GpsService.SERVICE_ID)) {
                // Add as GPS service listener.
                Service gpsService = gs.getServiceComm().getService(GpsService.SERVICE_ID);
                gpsService.addServiceListener(this);
            }
        }
    }

    @Override
    public void serviceUpdate(ServiceEvent event) {
        if (event.getType().equals(GpsService.GPS_UPDATE)) {
            // Update GPS location cache with new GPS location.
            Map<String, String> gpsInfo = (Map<String, String>) event.getTarget();
            double latitude = Double.parseDouble(gpsInfo.get("lat"));
            double longitude = Double.parseDouble(gpsInfo.get("lon"));
            GpsLocation gpsLoc = new GpsLocation(latitude, longitude);
            gpsLocCache.add(gpsLoc);
        }
    }

    @Override
    public boolean isDraggable() {
        return true;
    }
}